package guoanmaker.operator.business.controller;

import guoanmaker.operator.business.model.InRoundPhone;
import guoanmaker.operator.business.model.OperatorHotServer;
import guoanmaker.operator.business.service.OperatorHotServerServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.*;

/**
 *  
 *
 * @author huZhaoyu 
 * @ClassName: OperatorHotServerController
 * @Description: OperatorHotServerController 
 * @date 2017/6/29 上午10:11 
 */
@RestController
@RequestMapping(ApiController.OPERATOR_HOT_SERVER)
public class OperatorHotServerController {
    private final OperatorHotServerServiceInterface operatorHotServerServiceInterface;

    public OperatorHotServerController( OperatorHotServerServiceInterface operatorHotServerServiceInterface) {
        this.operatorHotServerServiceInterface = operatorHotServerServiceInterface;
    }

    /** 
    * @Title: saveHotServer
    * @Description: 新增/修改热门服务 
    * @param: OperatorHotServer   
    * @return AjaxJson    
    * @throws 
    */
    @ApiOperation(value = "保存热门服务", notes = "新增/修改热门服务")
    @PostMapping(value = "saveHotServer")
    public AjaxJson saveHotServer(@RequestBody InRoundPhone inRoundPhone) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	OperatorHotServer[] OperatorHotServers=inRoundPhone.getOperatorHotServers();
    	
    	operatorHotServerServiceInterface.delectAllBycitycode(inRoundPhone.getCityId());
    	for(OperatorHotServer hotserver:OperatorHotServers){
    		/*if(null!=hotserver.getId()&&!hotserver.getId().equals("")){
    			operatorHotServerServiceInterface.deleteHotServerById(hotserver.getId());
    		}*/
    		hotserver.setCityId(inRoundPhone.getCityId());
    		hotserver.setCreateDate(new Date());
    		operatorHotServerServiceInterface.saveHotServer(hotserver);
    	}
    	ajaxJson.setSuccess(true).setStatus(200).setData(null);
    	}catch(Exception e){
    		e.printStackTrace();
    		ajaxJson.setSuccess(false).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }

    /** 
     * @Title: deleteHotServerById
     * @Description: 根据Id删除热门服务
     * @param: String id  
     * @return AjaxJson 
     * @throws 
     */
    @ApiOperation(value = "删除热门服务", notes = "根据Id删除热门服务")
    @DeleteMapping(value = "delete/{id}")
    public AjaxJson deleteHotServerById(@PathVariable(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        operatorHotServerServiceInterface.deleteHotServerById(id);
        return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
    }

    /**
     * @return OperatorHotServer    
     * @Title: getHotServerById 
     * @Description: 根据Id查询热门服务
     * @param: String id   
     * @throws 
     */
    @ApiOperation(value = "根据Id查询热门服务", notes = "根据Id查询热门服务")
    @GetMapping(value = "getHotServerById")
    public AjaxJson getHotServerById(@RequestParam(value = "id") String id) {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotServerServiceInterface.getHotServerById(id));
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHotServer
     * @Description: 查找所有热门服务 
     * @throws 
     */
    @ApiOperation(value = "查找所有热门服务", notes = "查找所有热门服务")
    @GetMapping(value = "getAllHotServer")
    public AjaxJson getAllHotServer() {
    	AjaxJson ajaxJson=new AjaxJson();
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotServerServiceInterface.getAllHotServer());
    }

    /**
     * @return AjaxJson   
     * @Title: getAllHotServerByPage 
     * @Description: 查询所有热门服务并分页
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "分页查询热门服务", notes = "分页查询热门服务")
    @GetMapping(value = "getAllHotServerByPage")
    public AjaxJson getAllHotServerByPage(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotServerServiceInterface.getAllHotServerByPage(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getHotServerByPageOrderByWeight 
     * @Description: 按权重排序查询热门服务并分页 
     * @param: Integer page 页码
     * @param: Integer size 条数
     * @throws 
     */
    @ApiOperation(value = "按权重排序查询热门服务并分页", notes = "按权重排序查询热门服务并分页")
    @GetMapping(value = "getHotServerOrderByWeight")
    public AjaxJson getHotServerByPageOrderByWeight(@RequestParam(value = "page", defaultValue = "0") Integer page,
                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
    	AjaxJson ajaxJson=new AjaxJson();
    	return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotServerServiceInterface.getHotServerByPageOrderByWeight(page, size));
    }

    /**
     * @return AjaxJson   
     * @Title: getAll2App 
     * @Description: 查询所有并查询服务名称 
     */
   /* @ApiOperation(value = "查询所有并查询服务名称", notes = "查询所有并查询服务名称")
    @GetMapping(value = "getAll2App")
    public AjaxJson getAllAndServerName() {
        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorHotServerServiceInterface.getAllAndServerName());
    }*/
    
    /**
     * @return AjaxJson   
     * @Title: getAll2App 
     * @Description: 查询所有并查询服务名称 
     */
    @ApiOperation(value = "根据code查询所有热门服务", notes = "根据code查询所有热门服务")
    @PostMapping(value = "getAllhotServerByCityId")
    public AjaxJson getAllhotServerByCityId(String cityId) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	List<OperatorHotServer> hotservers=operatorHotServerServiceInterface.getAllAndServerName(cityId);
    	
    		ajaxJson.setSuccess(true).setStatus(200).setData(hotservers);
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }
    
    
    @ApiOperation(value = "根据cityid为11查询所有热门服务", notes = "根据cityid为11查询所有热门服务")
    @PostMapping(value = "getAllhotServerByElevenCityId")
    public AjaxJson getAllhotServerByElevenCityId(String cityId) {
    	AjaxJson ajaxJson=new AjaxJson();
    	try{
    	List<OperatorHotServer> hotservers=operatorHotServerServiceInterface.getAllhotServerByNull(cityId);
    	
    		ajaxJson.setSuccess(true).setStatus(200).setData(hotservers);
    	}catch(Exception e){
    		ajaxJson.setSuccess(true).setStatus(250).setData(null);
    	}
        return ajaxJson;
    }
}
